package com.cly.house.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cly.house.dao.HtDao;
import com.cly.house.dao.impl.HtDaoImpl;
import com.cly.house.entity.Ht;
import com.cly.house.uitl.PageInfo;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class HtController
 */
@WebServlet("/HtController")
public class HtController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private HtDao htDao = new HtDaoImpl();
		
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HtController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		switch (type) {
		case "list":
			list(request, response);
			break;

		default:
			break;
		}
	}
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String current = request.getParameter("current");
		int pageNum =1;
		if(current!=null) {
			pageNum = Integer.parseInt(current);
		}
		PageInfo<Ht> pageInfo = htDao.findByPage(pageNum, 3);
		String jsonStr = new ObjectMapper().writeValueAsString(pageInfo);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(jsonStr);
	}

}
